package com.nervenets.shopping.controller.common;

import com.nervenets.general.entity.ResponseResult;
import com.nervenets.general.jwt.util.JwtUtils;
import com.nervenets.general.model.SecurityUser;
import com.nervenets.general.web.BaseEntityController;
import com.nervenets.general.web.params.IdParams;
import com.nervenets.shopping.controller.common.params.AddressFormParams;
import com.nervenets.shopping.controller.common.params.AddressPagingParams;
import com.nervenets.shopping.hibernate.domain.common.Address;
import com.nervenets.shopping.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Api(value = "地址接口v1", tags = "个人信息 - 地址相关接口")
@RestController
@RequestMapping("/api/address")
public class AddressController extends BaseEntityController<Address, AddressService, AddressFormParams, AddressPagingParams, Address, Address> {

    @ApiOperation(value = "用户地址设置为默认")
    @PostMapping("/def")
    public ResponseEntity<ResponseResult<Void>> def(@Valid @RequestBody IdParams params) {
        final Address address = service.findOne(params.getTargetId(), "参数错误，地址不存在");

        SecurityUser user = JwtUtils.getUser();
        service.updateOtherAddressDefToFalse(user.getId(), address);
        return successMessage();
    }

}
